package com.webdemo9.contrller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 安全退出系统，去除session，清空Cookie
 * @author lovemanai
 *
 */
public class Exit extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession();
		String username = session.getAttribute("username").toString();
		session.removeAttribute("username");
		Cookie[] cookie = request.getCookies();
		for(int i = 0; i < cookie.length; i++ ) {
			if(cookie[i].getValue().equals(username)) {
				cookie[i].setMaxAge(-1);
			}
		}
		response.sendRedirect("index.jsp");
	}

}
